<?php

namespace App\Http\Controllers;

use App\Models\Schedule;
use App\Models\ScheduleRecord;
use App\Services\ProjectService;
use App\Services\ScheduleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ScheduleController extends Controller
{
    protected $ScheduleService;
    public function __construct(ScheduleService $ScheduleService)
    {
        $this->ScheduleService = $ScheduleService;
    }

    public function index(Request $Request) {
        $data = $this->ScheduleService->get('');
        if($Request->id){
            $data = $data->where('person_id',$Request->id);
        }
        $data = $data->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function statistics(Request $Request) {
        $res = $this->ScheduleService->statistics($Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$res['data'],'region'=>$res['region'],'title'=>$res['title'],'tomorrow'=>$res['tomorrow'],'day'=>$res['day'],'config'=>$res['config']]);
    }

    public function complete(Request $Request) {
        $date = Date('Y-m-d H:i:s',time());
        $sql = ScheduleRecord::with('schedule')->whereIn('type',[1,3])->orderBy('created_at','desc');
        if($Request->type == 'today'){
            $sql->whereDate('front_date','<=',$date)->whereDate('front_date','>=',$date);
        }
        $data = $sql->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Schedule::with('records')->with('user')->where('id',$Request->id)->first();
        $project = new ProjectService();
        $data->roleUsers = $project->getRoleUsers($data->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }
}
